package com.fishing.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.fishing.entity.Admin;
import com.fishing.entity.Auth;
import com.fishing.entity.AuthMap;
import com.fishing.entity.Role;
import com.fishing.entity.ZTreeModel;
import com.fishing.mapper.RoleMapper;
import com.fishing.service.RoleService;
@Service
public class RoleServiceImpl implements RoleService{

	@Autowired
	private RoleMapper roleMapper;
	@Override
	public int deleteByPrimaryKey(Integer id)throws Exception {
		return this.roleMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(Role record, String [] adminIds)throws Exception {
		int result = this.roleMapper.insert(record);
		
		if(null != adminIds && adminIds.length > 0){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("roleId", record.getId());
			map.put("adminIds", adminIds);
			this.roleMapper.insertRoleAdmins(map);
		}
		return result;
	}

	@Override
	public Role selectByPrimaryKey(Integer id)throws Exception {
		return this.roleMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Role> selectAll(Map<String,Object> map)throws Exception {
		return this.roleMapper.selectAll(map);
	}

	@Override
	public int updateByPrimaryKey(Role record, String [] adminIds)throws Exception {
		int result = this.roleMapper.updateByPrimaryKey(record);
		Integer roleId = record.getId();
		
		this.roleMapper.deleteAdminsByRoleId(roleId);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("roleId", roleId);
		map.put("adminIds", adminIds);
		this.roleMapper.insertRoleAdmins(map);
		return result;
	}

	@Override
	public List<Admin> selectRoleAdmins(Integer roleId) throws Exception {
		return this.roleMapper.selectRoleAdmins(roleId);
	}

	@Override
	public int insertRoleAdmins(Integer roleId, String[] adminIds) throws Exception {
		this.roleMapper.deleteAdminsByRoleId(roleId);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("roleId", roleId);
		map.put("adminIds", adminIds);
		return this.roleMapper.insertRoleAdmins(map);
	}

	@Override
	public List<String> queryAuthPids(String pid) throws Exception {
		return this.roleMapper.queryAuthPids(pid);
	}

	@Override
	public int insertRoleAuths(String ids, Integer roleId) throws Exception {
		int result = this.roleMapper.deleteAuthsByRoleId(roleId);
		
		if(!StringUtils.isEmpty(ids)){
			String [] authIds = ids.split(",");
			
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("roleId", roleId);
			map.put("authIds", authIds);
			this.roleMapper.insertRoleAuths(map);
		}
		return result;
		
	}

	@Override
	public List<ZTreeModel> queryAuthsByPid(String pid) throws Exception {
		return this.roleMapper.queryAuthsByPid(pid);
	}

	@Override
	public Auth getAuthById(String id) throws Exception {
		return this.roleMapper.getAuthById(id);
	}

	@Override
	public List<String> queryAuthsByRoleId(Integer roleId) throws Exception {
		return this.roleMapper.queryAuthsByRoleId(roleId);
	}

	@Override
	public List<AuthMap> queryAuthMaps(String authId, Integer adminId) throws Exception {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("authId", authId); //权限ID
		map.put("adminId", adminId);
		return this.roleMapper.queryAuthMaps(map);
	}
	
	public static void main(String[] args) {
		String [] strs = new String[]{"1","2","3"};
		String str = ArrayUtils.toString(strs);
		System.out.println(str.substring(1,str.length()-1));
	}

	@Override
	public String queryAuthsByAdminId(Integer adminId) throws Exception {
		List<String> authIdList = this.roleMapper.queryAuthsByAdminId(adminId);
		String str = "";
		if(null != authIdList && authIdList.size()>0){
			str = ArrayUtils.toString(authIdList.toArray());
			str = str.substring(1,str.length()-1);
		}
		return str;
	}

}
